package com.dxl.lanju.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.common.annotation.RequirePermission;
import com.dxl.common.consts.SysConsts;
import com.dxl.common.enums.State;
import com.dxl.common.model.Order;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.service.UserService;
import com.dxl.web.common.controller.BaseController;
import com.lanju.entity.User;

/**
 * 用户信息控制器
 * @author 邓小林
 */
@Controller
@RequestMapping("/user")
@RequirePermission(value={"USER-MODULE"}, name="用户模块", desc="包括用户增、删、改、查及授权")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("")
	@RequirePermission(value={"USER-VIEW"}, name="用户查看", desc="查看用户信息")
	public ModelAndView user() {
		ModelAndView mv = new ModelAndView("user/list");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/list")
	@RequirePermission(value={"USER-VIEW"}, name="用户查看", desc="查看用户信息")
	public Page<User> list(String keyword, Integer state, Integer page, Integer rows) {
		Page<User> p = this.userService.list(keyword, state, page, rows);
		return p;
	}
	
	@ResponseBody
	@RequestMapping("/listAll")
	@RequirePermission(value={"USER-VIEW"}, name="用户查看", desc="查看用户信息")
	public List<User> listAll(Integer state) {
		return userService.listByProp(User.class, "state", Utils.isNull(state) ? State.ACTIVE.getState() : state, Order.asc("name"));
	}
	
	@RequestMapping("/edit")
	@RequirePermission(value={"USER-VIEW"}, name="用户查看", desc="查看用户信息")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView("user/edit");
		if (Utils.isNotBlank(id)) {
			User user = this.baseService.get(User.class, id);
			if (Utils.isNotNull(user)) {
				user.setPassword(SysConsts.SIX_STAR);
			}
			mv.addObject("entity", user);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	@RequirePermission(value={"USER-MODIFY"}, name="用户修改", desc="修改用户信息")
	public RespData save(User user) {
		return userService.save(user);
	}
	
	@ResponseBody
	@RequestMapping("/del")
	@RequirePermission(value={"USER-DEL"}, name="用户删除", desc="删除用户信息")
	public RespData del(String[] ids) {
		return userService.del(ids);
	}
	
	@ResponseBody
	@RequestMapping("/state")
	@RequirePermission(value={"USER-STATE"}, name="用户状态修改", desc="修改用户状态")
	public RespData state(String[] ids, Integer state) {
		return this.state(User.class, ids, state);
	}
}
